package com.ruoyi.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.domain.T06VO;
import com.ruoyi.common.result.Result;
import com.ruoyi.domain.T19;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IPcService;
import com.ruoyi.service.IT19Service;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.T06;
import com.ruoyi.service.IT06Service;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 授权中心Controller
 *
 * @author w
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/t06/t06")
public class T06Controller extends BaseController
{
    @Autowired
    private IT06Service t06Service;
    @Autowired
    private IT19Service t19Service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IPcService pcService;
     /**
     * 查询授权中心列表
     */
//    @PreAuthorize("@ss.hasPermi('authcenter:T06:list')")
    @GetMapping("list")
    @ApiOperation("人员列表")
    public TableDataInfo getIdentifyList(HttpServletRequest request)
    {
        startPage();
        Long userId=tokenService.getLoginUser(request).getUser().getUserId();
        SysUser user=sysUserService.selectUserById(userId);
        Long deptId=user.getDeptId();
        List<T06> list =  pcService.getUserList(userId);
        return getDataTable(list);
    }

    /**
     * 导出授权中心列表
     */
    @PreAuthorize("@ss.hasPermi('authcenter:T06:export')")
    @Log(title = "授权中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, T06 t06)
    {
        List<T06> list = t06Service.selectT06List();
        ExcelUtil<T06> util = new ExcelUtil<T06>(T06.class);
        util.exportExcel(response, list, "授权中心数据");
    }

    /**
     * 获取授权中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('authcenter:T06:query')")
    @GetMapping(value = "/{F0601}")
    public AjaxResult getInfo(@PathVariable("F0601") Long F0601)
    {
        return success(t06Service.selectT06ByF0601(F0601));
    }

    /**
     * 新增授权中心
     */
    @ApiOperation("新增用户")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody T06 t06)
    {
        return toAjax(t06Service.insertT06(t06));
    }

    /**
     * 修改授权中心
     */
    @PreAuthorize("@ss.hasPermi('authcenter:T06:edit')")
    @Log(title = "授权中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody T06 t06)
    {
        return toAjax(t06Service.updateT06(t06));
    }

    @PreAuthorize("@ss.hasPermi('authcenter:T06:remove')")
    @GetMapping("/getF0609")
    public Result<List<String>> selectF0609()
    {
        return Result.success(t06Service.selectF0609(),"请求成功");
    }
    @PutMapping("/updateByF0601s/{F0601s}")
    @ApiOperation("批量修改")
    public AjaxResult updateByF0601s(@PathVariable() Long[] F0601s, T06VO t06vo) {
        if(F0601s.length==0){
            return AjaxResult.warn("请选择人员");
        }
        Boolean flag = true;
        for(Long F0601 : F0601s ){
            T06 t06 =t06Service.selectT06ByF0601(F0601);
            if (t06 == null){
                flag= false;
                continue;
            }
            t06.setF0613(t06vo.getF0613());
            t06.setF0614(t06vo.getF0614());
            t06.setF0501(t06vo.getF0501());
            t06.setF0610(t06vo.getF0610());
            t06Service.updateT06(t06);
        }
        if(!flag){
            return AjaxResult.warn("部分人员信息不存在");
        }
        return AjaxResult.success();
    }
    @PutMapping("/registerApprove/approve")
    @ApiOperation("审批通过")
    public Result approve(@RequestParam Long[] F0601s, HttpServletRequest request){
        LoginUser loginUser =tokenService.getLoginUser(request);
        Long userId=loginUser.getUserId();
        if(F0601s.length==0){
            return Result.erro("请选择人员");
        }
        int i=0;
        String message="";
        for(Long F0601:F0601s){
            T06 t06 = null;
            T19 t19 = new T19();
            t19.setF1801(userId);
            t19.setF0601(F0601);
            t19.setF1902(1L);
            t19.setF1903(new Date());
            try {
                t06 = t06Service.selectT06ByF0601(F0601);
            } catch (Exception e) {
                message+="F0601:"+F0601+"审批失败\n";
                continue;
            }
            t06.setF0612(0L);
            int flag = 0;
            flag =t06Service.updateT06(t06);
            if(flag==0){
                message+="姓名:"+t06.getF0602()+"审批失败\n";
                t19.setF1905(1L);
                t19.setF1904("审批人员:"+t06.getF0602()+" 审批失败");
            }else {
                t19.setF1904("审批人员:"+t06.getF0602()+" 审批通过");
                t19.setF1905(0L);
                i++;
            }
            t19Service.insertT19(t19);
        }
        if(i==F0601s.length){
            return Result.success(i,"审批成功");
        }
        return new Result("审批失败",message);
    }
//    @PutMapping
//    public AjaxResult updateUser(@RequestBody T06 t06){
//        return AjaxResult.success(t06Service.updateT06(t06));
//    }
    @DeleteMapping("/{F0601s}")
    public AjaxResult remove(@PathVariable Long[] F0601s)
    {
        return toAjax(t06Service.deleteT06ByF0601s(F0601s));
    }
}
